package cn.com.hbhgkj.bussiness;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;

import cn.com.hbhgkj.common.DaoException;
import cn.com.hbhgkj.common.Pagination;
import cn.com.hbhgkj.common.ServiceException;
import cn.com.hbhgkj.config.Constants;
import cn.com.hbhgkj.dao.CopyrightAlterationDao;
import cn.com.hbhgkj.dao.CopyrightRegisterationDao;
import cn.com.hbhgkj.pojo.CopyrightAlteration;
import cn.com.hbhgkj.pojo.CopyrightRegisteration;
import cn.com.hbhgkj.pojo.User;
import cn.com.hbhgkj.utils.CopyrightSender;
import cn.com.hbhgkj.utils.NameValuePairsFactory;

public class CopyrightAlterationServiceImpl implements
		CopyrightAlterationService {

	public CopyrightAlteration loadById(long id) throws ServiceException {
		try {
			return getCopyrightAlterationDao().get(id);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public List<CopyrightAlteration> findByApplicant(User applicant,
			Pagination pagination) throws ServiceException {
		try {
			return getCopyrightAlterationDao().findByApplicant(applicant,
					pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public List<CopyrightAlteration> getReviwed(Pagination pagination)
			throws ServiceException {
		try {
			return copyrightAlterationDao.findByReviewCodes(new ReviewCode[] {
					ReviewCode.FAIL, ReviewCode.PASS }, pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public void submit(CopyrightAlteration copyrightAlteration)
			throws ServiceException {
		try {
			copyrightAlteration.setState(ReviewCode.PENDING_REVIEW.getValue());
			Date curDate = new Date();
			copyrightAlteration.setApplyTime(curDate);
			copyrightAlteration.setUpdateTime(curDate);
			copyrightAlteration.setLoginId(Constants.AGENT_USER_NAME);
			/*
			 * if (copyrightAlteration.getId() != null) { CopyrightAlteration cr
			 * = copyrightAlterationDao.get(copyrightAlteration.getId());
			 * PropertyUtils.copyProperties(cr, copyrightAlteration); } else {
			 */copyrightAlterationDao.saveOrUpdate(copyrightAlteration);
			// }
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	public List<CopyrightAlteration> getPendingReviews(Pagination pagination)
			throws ServiceException {
		try {
			return copyrightAlterationDao.findByReviewCode(
					ReviewCode.PENDING_REVIEW, pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public void review(User reviewer, CopyrightAlteration copyrightAlteration)
			throws ServiceException {
		try {
			// TODO validate reviewer if is valid
			CopyrightAlteration entity = copyrightAlterationDao
					.get(copyrightAlteration.getId());

			// 审核人更改
			String[] ignoreCopy = new String[] { "applicant", "applyTime",
					"loginId" };
			BeanUtils.copyProperties(copyrightAlteration, entity, ignoreCopy);

			entity.setState(copyrightAlteration.getState());
			entity.setReviewer(reviewer);
			entity.setUpdateTime(new Date());

			if (!ReviewCode.isPass(copyrightAlteration.getState()))
				return;

			// send form to copyright center
			CopyrightSender.login(Constants.AGENT_USER_NAME,
					Constants.AGENT_PASSWORD);

			NameValuePairsFactory paramsFactory = new NameValuePairsFactory(
					entity);

			// 发送表单至copyright中心
			// get serial number
			String serialNum = CopyrightSender.send(
					paramsFactory.createNameValuePairs(),
					CopyrightSender.r41PostUrl);
			CopyrightRegisteration cr = buildCopyrightRegisteration(serialNum,
					entity);
			// 保存我的登记
			copyrightRegisterationDao.save(cr);

		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	private CopyrightRegisteration buildCopyrightRegisteration(
			String serialNum, CopyrightAlteration copyright) {
		CopyrightRegisteration cr = new CopyrightRegisteration();
		cr.setApplyFormId(copyright.getId());
		cr.setApplyFormType(ApplicationFormType.R11.getValue());
		cr.setContact(copyright.getSqrcontactPerson());
		cr.setCopyrighter(copyright.getCopyrighter());
		cr.setEmail(copyright.getSqremail());
		cr.setPhoneNum(copyright.getSqrphone());
		cr.setProductName(copyright.getSoftFullName());
		cr.setMobileNum(copyright.getSqrmobile());
		cr.setSerialNum(serialNum);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		cr.setRegistDate(sdf.format(new Date()));
		return cr;
	}

	private CopyrightAlterationDao copyrightAlterationDao;
	private CopyrightRegisterationDao copyrightRegisterationDao;

	public CopyrightAlterationDao getCopyrightAlterationDao() {
		return copyrightAlterationDao;
	}

	public void setCopyrightAlterationDao(
			CopyrightAlterationDao copyrightAlterationDao) {
		this.copyrightAlterationDao = copyrightAlterationDao;
	}

	public CopyrightRegisterationDao getCopyrightRegisterationDao() {
		return copyrightRegisterationDao;
	}

	public void setCopyrightRegisterationDao(
			CopyrightRegisterationDao copyrightRegisterationDao) {
		this.copyrightRegisterationDao = copyrightRegisterationDao;
	}

}
